#include "Date.h"

#define _CRT_SECURE_NO_WARNINGS

void Date::Print()
{
	cout << _year << "/" << _month << "/" << _day << endl;
}

Date Date::operator+(int num)
{
	Date tem = *this;
	tem.getDateAdd(tem, num);
	return tem;
}

Date Date::operator-(int num)
{
	Date tem = *this;
	this->getDateDel(tem, num);
	return tem;
}

int Date::operator-(Date t)
{
	if (*this > t)
	{
		if (_year == t._year && _month == t._month)
		{
			return _day - t._day;
		}
		int day = _day;
		_month--;

		while (_year != t._year || _month != t._month - 1)
		{
			if (_month == 0)
			{
				_year--;
				_month = 12;
			}
			day += this->getDate();
			_month--;
		}
		day -= t._day;
		return day;
	}
	else
	{
		if (_year == t._year && _month == t._month)
		{
			return -(t._day - _day);
		}
		int day = t._day;
		t._month--;

		while (_year != t._year || t._month != _month - 1)
		{
			if (t._month == 0)
			{
				t._year--;
				t._month = 12;
			}
			day += t.getDate();
			t._month--;
		}
		day -= _day;
		return -day;
	}
	return 0;
}

Date& Date::operator+=(int num)
{
	this->getDateAdd(*this, num);
	return *this;
}

Date& Date::operator-=(int num)
{
	this->getDateDel(*this, num);
	return *this;
}

int Date::getDate()
{
	int arr[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	if (((_year % 4 == 0) && (_year % 100 != 0)) || (_year % 400 == 0))
		arr[2]++;
	return arr[this->_month];
}

void Date::getDateAdd(Date& t, int num)
{
	int arr[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	t._day += num;
	if (((_year % 4 == 0) && (_year % 100 != 0)) || (_year % 400 == 0))
		arr[2]++;
	while (t._day > t.getDate())
	{
		t._day -= t.getDate();
		t._month++;
		if (t._month == 13)
		{
			t._year++;
			t._month = 1;
		}
	}
}

void Date::getDateDel(Date& t, int num)
{
	int arr[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	t._day -= num;
	if (((_year % 4 == 0) && (_year % 100 != 0)) || (_year % 400 == 0))
		arr[2]++; 
	while (t._day < 0 || t._day > t.getDate())
	{
		t._month--;
		if (t._month == 0)
		{
			t._year--;
			t._month = 12;
		}
		t._day += t.getDate();
	}
}

Date& Date::operator++()
{
	*this += 1;
	return *this;
}

Date Date::operator++(int)
{
	Date tem(*this);
	*this += 1;
	return tem;
}

bool Date::operator==(const Date& t)
{
	return _year == t._year
		&& _month == t._month
		&& _day == t._day;
}

bool Date::operator>(const Date& t)
{
	//if (_year > t._year)
	//	return _year > t._year;
	//if (_year == t._year && _month > t._month)
	//	return _year == t._year && _month > t._month;
	//if (_year == t._year && _month == t._month && _day > t._day)
	//	return _year == t._year && _month == t._month && _day > t._day;
	//return false;

	return (_year > t._year) || (_year == t._year && _month > t._month) || (_year == t._year && _month == t._month && _day > t._day);
}

bool Date::operator>=(const Date& t)
{
	return *this > t || *this == t;
}

bool Date::operator<(const Date& t)
{
	return !(*this >= t);
}

bool Date::operator<=(const Date& t)
{
	return !(*this > t);
}

//
//void Date::operator<<(ostream& out)
//{
//	out << _year << "/" << _month << "/" << _day << endl;
//}

ostream operator<<(ostream& out, const Date& d)
{
	out << d._year << "/" << d._month << "/" << d._day << endl;
}

